/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-07 17:00:21
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-09 10:35:01
 */

import { Dictionary } from "../../utils/dictionary";
import gameCtr from "../gameCtr";
import netCtr from "../netCtr";
import { GuideElement } from "./GuideElement";

export default class guideCtr {

  private static _instance: guideCtr = null;
  public static get ins(): guideCtr {

    if (!guideCtr._instance) {

      guideCtr._instance = new guideCtr();
    }

    return guideCtr._instance;
  }

  public guideIndex: number = 0;

  private _guideDic: Dictionary<number, any> = new Dictionary();

  public isComplete: boolean = false;

  public guideJosn;

  public initConfData() {

    let vo: GuideElement;
    let data: any = this.guideJosn;

    data.forEach((guide, i) => {
      vo = new GuideElement()
      vo.whetherForce = guide.whetherForce;
      vo.talk = guide.talk;
      vo.id = guide.id;
      vo.nextId = guide.nextId;
      vo.layer = guide.layer;
      vo.rectNode = guide.rectNode;
      vo.condition = Boolean(guide.condition);
      vo.path = guide.path;
      vo.must = Number(guide.must);

      if (guide.guidePosition && guide.guidePosition.length) {
        vo.guidePosition.x = parseInt(guide.guidePosition.split(",")[0]);
        vo.guidePosition.y = guide.guidePosition.split(",")[1];

        vo.talkPosition.x = guide.talkPosition.split(",")[0];
        vo.talkPosition.y = guide.talkPosition.split(",")[1];

        vo.arrowPosition.x = guide.arrowPosition.split(",")[0];
        vo.arrowPosition.y = guide.arrowPosition.split(",")[1];

        vo.arrowRote = guide.arrowPosition.split(",")[2];
      }

      this._guideDic.set(vo.id, vo);
    });

  }

  public setNextGuideIndex() {

    if (this.getCurrentGuide()) {

      this.guideIndex = this.getCurrentGuide().nextId;
      if (this.guideIndex == 0) {
        gameCtr.ins.guideId++;
        netCtr.ins.guide();
      }
      // console.log(this.guideIndex, "this.guideIndex");

    }
  }

  public getCurrentGuide() {

    return this._guideDic.get(this.guideIndex);
  }

  public getCurrentGuidebyId(id) {

    return this._guideDic.get(id);
  }

  public getGuideLen() {

    return this._guideDic.length();
  }
}